package com.fzl.spring_chatroom.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

//创建一个类，继承TextWebSocketHandler（属于spring内置）
//加上@Component注解，注册到spring，交给spring管理
//继承TextWebSocketHandler目的是结成其里卖弄的方法
//WebSocketSession连接中的会话
@Component
public class TestWebSockedController extends TextWebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        //websocket连接成功后成功调用
        System.out.println("连接成功");
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        //有异常调用
        System.out.println("收到异常");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        //正常关闭
        System.out.println("关闭成功");
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        //收到消息被调用
        System.out.println("收到消息成功");
        session.sendMessage(message);
    }
}
